import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';
import { Component, OnInit } from '@angular/core';
// import { ToastService, UploadConfig } from 'ng-tui';
import { GlobalService, AuthService } from '../../../../cores/services';
import { SaleService } from '../../services/sale.service';
import { Manage } from '../../interfaces/manage.interface';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService, Item, UploadConfig } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { Goods, Term } from '../../interfaces/manage.interface';

import { skipWhile, switchMap } from 'rxjs/operators';
import { of } from 'rxjs';
import { delay } from 'rxjs/operators';

@Component({
    templateUrl: './carpay-detail.component.html',
    providers: [Location, { provide: LocationStrategy, useClass: PathLocationStrategy }],
})
export class CarPayDetailComponent implements OnInit {
    thFir = ['#', '单据号码', '单据类型', '客户名称', '单据数量', '单据金额', '单据日期'];
    thSec = ['月份', '1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月', '合计'];
    payTerms: Array<Term> = [
        {
            beforeInputLabel: '款到发货',
            hasInput: false,

        },
        {
            beforeInputLabel: '货到付款',
            hasInput: false,
        },
        {
            beforeInputLabel: '实销实结',
            hasInput: false,
        },
        {
            beforeInputLabel: '月结',
            afterInputLable: '天',
            hasInput: true,
            inputValue: ''
        }, {
            beforeInputLabel: '票结',
            afterInputLable: '天',
            hasInput: true,
            inputValue: ''
        },
        {
            beforeInputLabel: '铺底',
            afterInputLable: '元',
            hasInput: true,
            inputValue: ''
        },
        {
            beforeInputLabel: '滚单结算',
            hasInput: false,
        },
        {
            beforeInputLabel: '售价',
            afterInputLable: '折',
            hasInput: true,
            inputValue: ''
        }];
    freightTerm: Array<Term> = [{
        beforeInputLabel: '含税到岸',
        hasInput: false,
    },
    {
        beforeInputLabel: '含税到仓',
        hasInput: false,
    }, {
        beforeInputLabel: '到岸垫付',
         hasInput: false,
    },
    {
        beforeInputLabel: '到仓垫付',
         hasInput: false,
    }, {
        beforeInputLabel: '送货到仓',
         hasInput: false,
    },
    {
        beforeInputLabel: '运费自担',
         hasInput: false,
    }];
    returnTerm: Array<Term> = [{
        beforeInputLabel: '无条件退货',
         hasInput: false,
    }, {
        beforeInputLabel: '质劣退货',
         hasInput: false,
    },
    {
        afterInputLable: '折退货',
        hasInput: true,
        inputValue: ''
    }, {
        beforeInputLabel: '过保不退',
         hasInput: false,
    }
    ];
    rebateTerm: Array<Term> = [{
        beforeInputLabel: '无条件返利',
        afterInputLable: '%',
        hasInput: true,
        inputValue: ''
    }, {
        beforeInputLabel: '有条件返利',
         hasInput: false,
    },
    {
        beforeInputLabel: '月返',
          afterInputLable: '%',
        hasInput: true,
        inputValue: ''
    }, {
        beforeInputLabel: '季返',
          afterInputLable: '%',
        hasInput: true,
        inputValue: ''
    },
    {
        beforeInputLabel: '年返',
          afterInputLable: '%',
        hasInput: true,
        inputValue: ''
    }];
    search = new SearchParams({ start: '', end: '' });

    list = new Array<Goods>();

    loading = false;

    pagination = new Pagination();
    parentTypes = new Array<Item>();
    childTypes = new Array<any>();
    goodsThreeType = new Array<any>();
    goods: Goods = { id: 0, isActive: 1, goodsName: '' };


    get getChildTypes(): Item[] {
        return this.childTypes.filter(type => type.parentId === this.goods.goodsParentType);
    }
    get getThreeTypes(): Item[] {
        return this.childTypes.filter(type => type.parentId === this.goods.goodsParentType);
    }
    config: UploadConfig = {
        host: '',
        uploader: file => of('https://picsum.photos/300/300/?random').pipe(delay(2000))
    };

    imageUrl = 'https://picsum.photos/300/300?100';

    imagesUrl = 'https://picsum.photos/300/300?200,https://picsum.photos/300/300?300';
    constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        private confirm: ConfirmService,
        private toast: ToastService,
        public global: GlobalService,
        private manageService: SaleService
    ) { }

    ngOnInit() {
        this.activatedRoute.url
            .pipe(skipWhile(() => this.router.url !== '/manage/account'))
            .subscribe(() => this.loadDatas());
    }

    // doSearch() {
    //     this.pagination.reset();
    //     this.loadDatas();
    // }

    // doReset() {
    //     this.search.clean();
    //     this.doSearch();
    // }

    loadDatas() {
        this.loading = true;
        this.manageService.searchGoods(this.pagination, this.search)
            .subscribe({
                next: res => {
                    this.pagination.total = res.datas.total;
                    this.list = res.datas.rows;
                },
                complete: () => this.loading = false
            });
    }

    // confirmDelete(goods: Goods) {
    //     this.confirm.danger('删除确认', `您确认删除商品'${goods.goodsName}'吗？`)
    //         .pipe(switchMap<void, ApiData>(() => this.manageService.deleteGoods(goods.id)))
    //         .subscribe(() => {
    //             this.list.splice(this.list.indexOf(goods), 1);
    //             this.toast.success('删除成功', `成功删除商品'${goods.goodsName}`);
    //             this.loadDatas();
    //         });
    // }

}
